@extends('admin.layouts.app')

@section('title', '角色管理')

@section('content')
<div class="container-fluid">
    <!-- 页面头部 -->
    <div class="d-flex justify-content-between align-items-center mb-4 mt-3">
        <div>
            <h3 class="fw-bold text-dark mb-1">角色管理</h3>
            <p class="text-muted mb-0">管理系统角色和权限分配</p>
        </div>
        @can('create-roles')
        <a href="{{ route('admin.roles.create') }}" class="btn btn-primary">
            <i class="fas fa-plus me-2"></i>创建角色
        </a>
        @endcan
    </div>

    <!-- 消息提示 -->
    @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            <i class="fas fa-check-circle me-2"></i>
            {{ session('success') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    @endif

    <!-- 主要内容区域 - 自适应布局 -->
    <div class="row g-4">
        <!-- 左侧 - 角色列表 -->
        <div class="col-xxl-9 col-xl-8 col-lg-8 col-md-12">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-header bg-white border-0 py-3">
                    <h5 class="fw-bold mb-0">
                        <i class="fas fa-user-tag text-primary me-2"></i>
                        角色列表
                    </h5>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th class="border-0">ID</th>
                                    <th class="border-0">角色名称</th>
                                    <th class="border-0">权限数量</th>
                                    <th class="border-0">用户数量</th>
                                    <th class="border-0">创建时间</th>
                                    <th class="border-0">操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse($roles as $role)
                                <tr>
                                    <td class="fw-medium">#{{ $role->id }}</td>
                                    <td>
                                        <div class="d-flex align-items-center">
                                            <div class="badge bg-primary bg-opacity-10 text-primary rounded-circle me-2" style="width: 32px; height: 32px; display: flex; align-items: center; justify-content: center;">
                                                <i class="fas fa-user-tag"></i>
                                            </div>
                                            <span class="fw-semibold">{{ $role->name }}</span>
                                        </div>
                                    </td>
                                    <td>
                                        <span class="badge bg-info">{{ $role->permissions->count() }} 个权限</span>
                                    </td>
                                    <td>
                                        <span class="badge bg-success">{{ $role->users->count() }} 个用户</span>
                                    </td>
                                    <td class="text-muted">{{ $role->created_at->format('Y-m-d H:i') }}</td>
                                    <td>
                                        <div class="btn-group" role="group">
                                            <a href="{{ route('admin.roles.show', $role) }}" class="btn btn-sm btn-outline-info" title="查看详情">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                            @can('edit-roles')
                                            <a href="{{ route('admin.roles.edit', $role) }}" class="btn btn-sm btn-outline-warning" title="编辑角色">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                            @endcan
                                            @can('delete-roles')
                                            <form action="{{ route('admin.roles.destroy', $role) }}" method="POST" 
                                                  style="display: inline-block;" 
                                                  onsubmit="return confirm('确定要删除这个角色吗？')">
                                                @csrf
                                                @method('DELETE')
                                                <button type="submit" class="btn btn-sm btn-outline-danger" title="删除角色">
                                                    <i class="fas fa-trash"></i>
                                                </button>
                                            </form>
                                            @endcan
                                        </div>
                                    </td>
                                </tr>
                                @empty
                                <tr>
                                    <td colspan="6" class="text-center py-4">
                                        <div class="text-muted">
                                            <i class="fas fa-inbox fs-3 d-block mb-2"></i>
                                            暂无角色数据
                                        </div>
                                    </td>
                                </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                    
                    @if($roles->hasPages())
                    <div class="mt-4">
                        <div class="pagination-info">
                            显示第 {{ $roles->firstItem() }} - {{ $roles->lastItem() }} 条，共 {{ $roles->total() }} 条记录
                        </div>
                        <div class="d-flex justify-content-center">
                            {{ $roles->links() }}
                        </div>
                    </div>
                    @endif
                </div>
            </div>
        </div>
        
        <!-- 右侧 - 角色统计和快速操作 -->
        <div class="col-xxl-3 col-xl-4 col-lg-4 col-md-12">
            <!-- 角色统计 -->
            <div class="card border-0 shadow-sm mb-4">
                <div class="card-header bg-white border-0 py-3">
                    <h6 class="fw-bold mb-0">
                        <i class="fas fa-chart-pie text-info me-2"></i>
                        角色统计
                    </h6>
                </div>
                <div class="card-body">
                    <div class="d-flex align-items-center justify-content-between mb-3">
                        <span class="text-muted">总角色数</span>
                        <span class="badge bg-primary">{{ $roles->total() }}</span>
                    </div>
                    <div class="d-flex align-items-center justify-content-between mb-3">
                        <span class="text-muted">系统角色</span>
                        <span class="badge bg-warning">{{ $roles->where('name', 'like', '%admin%')->count() + $roles->where('name', 'like', '%管理员%')->count() }}</span>
                    </div>
                    <div class="d-flex align-items-center justify-content-between">
                        <span class="text-muted">普通角色</span>
                        <span class="badge bg-success">{{ $roles->total() - ($roles->where('name', 'like', '%admin%')->count() + $roles->where('name', 'like', '%管理员%')->count()) }}</span>
                    </div>
                </div>
            </div>

            <!-- 快速操作 -->
            <div class="card border-0 shadow-sm">
                <div class="card-header bg-white border-0 py-3">
                    <h6 class="fw-bold mb-0">
                        <i class="fas fa-bolt text-warning me-2"></i>
                        快速操作
                    </h6>
                </div>
                <div class="card-body">
                    @can('create-roles')
                    <a href="{{ route('admin.roles.create') }}" class="btn btn-outline-primary w-100 mb-2">
                        <i class="fas fa-plus me-2"></i>
                        创建新角色
                    </a>
                    @endcan
                    
                    @can('view-permissions')
                    <a href="{{ route('admin.permissions.index') }}" class="btn btn-outline-info w-100 mb-2">
                        <i class="fas fa-key me-2"></i>
                        权限管理
                    </a>
                    @endcan
                    
                    @can('view-users')
                    <a href="{{ route('admin.users.index') }}" class="btn btn-outline-success w-100">
                        <i class="fas fa-users me-2"></i>
                        用户管理
                    </a>
                    @endcan
                </div>
            </div>
        </div>
    </div>
</div>

<style>
    .table th {
        font-weight: 600;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        font-size: 0.75rem;
        color: #6b7280;
        background-color: #f9fafb;
    }
    
    .table tbody tr:hover {
        background-color: #f8fafc;
    }
    
    .btn-group .btn {
        margin: 0 1px;
    }
    
    .badge {
        font-weight: 500;
    }
</style>
@endsection